package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.sdata.App;
import com.xhwl.common.pojo.cent.sdata.ProjectApp;
import com.xhwl.data.dao.IAppDao;
import com.xhwl.data.dao.IProjectAppDao;
import com.xhwl.data.service.IProjectAppService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 项目与app
 * @author: lianghz
 * @create: 2020-11-25
 **/
@Service
public class ProjectAppServiceImpl extends ServiceImpl<IProjectAppDao, ProjectApp> implements IProjectAppService {

    @Autowired
    IProjectAppDao projectAppDao;

    @Autowired
    private IAppDao appDao;

    private static final Logger log = LoggerFactory.getLogger(ProjectAppServiceImpl.class);

    @Override
    public void save(Integer projectId, List<Integer> appIdList) {
        QueryWrapper<ProjectApp> projectAppQueryWrapper = new QueryWrapper<>();
        projectAppQueryWrapper.eq("project_id", projectId);
        this.remove(projectAppQueryWrapper);
        List<ProjectApp> projectApps = appIdList.stream().map(appId -> {
            ProjectApp projectApp = new ProjectApp();
            projectApp.setAppId(appId);
            projectApp.setProjectId(projectId);
            return projectApp;
        }).collect(Collectors.toList());
        this.saveBatch(projectApps);
    }

    @Override
    public List<Integer> list(Integer projectId) {
        QueryWrapper<ProjectApp> projectAppQueryWrapper = new QueryWrapper<>();
        projectAppQueryWrapper
                .select("app_id")
                .eq("project_id", projectId);
        List<ProjectApp> projectApps = this.list(projectAppQueryWrapper);
        List<Integer> appIdList = projectApps.stream().map(ProjectApp::getAppId).collect(Collectors.toList());
        return appIdList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProjectApp(List<Integer> projectIdList, List<Integer> appIdList) {
        try {
            UpdateWrapper updateWrapper = new UpdateWrapper();
            if (!CollectionUtils.isEmpty(projectIdList)) {
                updateWrapper.in("project_id", projectIdList);
            }

            if (!CollectionUtils.isEmpty(appIdList)) {
                updateWrapper.in("app_id", appIdList);
            }

            projectAppDao.delete(updateWrapper);

        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new BusinessException("删除项目App关联表失败");
        }
    }

    @Override
    public List<Integer> findProjectIdByAppId(String appId) {
        if(StringUtils.isEmpty(appId)){
            log.warn("无效的参数");
            return Collections.emptyList();
        }
        QueryWrapper<App> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id", appId);
        App app = appDao.selectOne(queryWrapper);
        if(null == app){
            log.warn("app {} 信息不存在", appId);
            return Collections.emptyList();
        }
        QueryWrapper<ProjectApp> projectAppQueryWrapper = new QueryWrapper<>();
        projectAppQueryWrapper.eq("app_id", app.getId());
        List<ProjectApp> projectApps = projectAppDao.selectList(projectAppQueryWrapper);
        if(!projectApps.isEmpty()){
            return projectApps.stream().map(i -> i.getProjectId()).collect(Collectors.toList());
        }
        log.info("app {} 暂未关联项目信息", appId);
        return Collections.emptyList();
    }

}
